# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import re
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp.addons.dm_base import utils

class product_product(osv.osv):
	_inherit = "product.product"
	_columns = {
	    'solder_cnt':fields.integer('焊点数', track_visibility='onchange'),
	    'solder_method':fields.selection([('ai_ver','立式AI'),('ai_hor','卧式AI'),('smt','SMT'),('resize','整形'),('rivet','打铆钉'),('none','无')],'焊点工序', track_visibility='onchange', required=False),
	    #PCB
	    'pcb_file': fields.function(utils.field_get_file, fnct_inv=utils.field_set_file,  type='binary',  string='PCB', multi="_get_file"),
	    'pcb_file_name': fields.char('PCB文件名'),
	    #原理图
	    'schematic_diagram_file': fields.function(utils.field_get_file, fnct_inv=utils.field_set_file,  type='binary',  string='原理图', multi="_get_file"),
	    'schematic_diagram_file_name': fields.char('原理图'),
	}
	_defaults={'solder_method':'none'}
		
	def _categ_code_hook(self, cr, uid, categ_code, context=None):
		len_empty_zero = 7 - len(categ_code)
		if len_empty_zero > 0:
			empty_zero = ''
			for i in range(0,len_empty_zero):
				empty_zero += '0'
			categ_code += empty_zero
		return categ_code
	
	def _seq_len(self, cr, uid, context=None):
		return 4
	
	def _conn_char(self, cr, uid, context=None):
		return ''
		
	def _prod_last_id(self, cr, uid, categ_id, categ_code, context=None):
		#按照前五位递增序列号, 后4位为序列号
		cr.execute('''
		select id
		from product_product 
		where default_code ilike %s
		order by right(default_code,4) desc limit 1
		''',(categ_code[:5]+'%',))
		row = cr.fetchone()
		prod_last_id = row and row[0] or False
		return prod_last_id

	def _check_write_vals(self,cr,uid,vals,ids=None,context=None):
		resu = super(product_product,self)._check_write_vals(cr, uid, vals, ids, context=context)
		if vals.get('default_code') or vals.get('categ_id'):
			#获得default_code
			default_code = vals.get('default_code')
			if not default_code:
				default_code = self.read(cr, uid, ids[0], ['default_code'])['default_code']
			#获得categ_code
			categ_id = vals.get('categ_id')
			if not categ_id:
				categ_id = self.read(cr, uid, ids[0], ['categ_id'])['categ_id'][0]
			categ_code = self.pool.get('product.category').read(cr, uid, categ_id, ['code'])['code']
			#匹配检查
			#1.编码位数限制
			prod_code_len = 11
			if len(default_code) != prod_code_len:
				raise osv.except_osv('错误','物料编码"%s"位数不是%s位!'%(default_code,prod_code_len))
			#2.编码前缀和产品分类要匹配
			if not default_code.startswith(categ_code):
				raise osv.except_osv('错误','物料编码"%s"的前缀和产品分类编码"%s"不匹配!'%(default_code,categ_code))
		return resu

	def onchange_categ_id(self, cr, uid, ids, categ_id, context=None):
		res = super(product_product, self).onchange_categ_id(cr, uid, ids, categ_id)
		if categ_id:
			categ = self.pool.get('product.category').read(cr, uid, categ_id, ['name'],context=context)
			res.get('value').update({'name':categ['name']})
			
		return res
product_product()
